package com.cspm.ahjy.bc.office.word.domain;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 包装MSAction的类型
 * @author ServerZhang
 * @date 2017年10月16日
 */
public class MSActionType {

	/**
	 * 操作的类型，比如：
	 * 上边距为“body_top”、
	 * 页面上边框颜色为“body_topBorder_color”、
	 * 字体加粗为“font_bold”
	 */
	private String type = null;
	/**
	 * 操作类型的描述
	 */
	private String typeContent = "";
	/**
	 * 操作类型对应的属性值，其类型对应MS对象对应的属性类型
	 */
	private Object typeValue = null;
	
	
	public Boolean equals(MSActionType actionType){
		if (actionType == null) {
			return Boolean.FALSE;
		}
		String type2 = actionType.getType();
		Object typeValue2 = actionType.getTypeValue();
		
		if (this.type != null && this.type.equals(type2)
				&& this.typeValue != null ) {
			//简单对象的Object可直接使用equals，但复杂对象需要将object先强制转换后再使用重写的equals方法
			switch (this.type) {
			case "paragraph_msItem":
				MSItem typeItem1 = null;
				MSItem typeItem2 = null;
				
				String typeValue1Str = this.typeValue.toString();
				String typeValue2Str = typeValue2.toString();
				
				if(typeValue1Str.startsWith("{") && typeValue1Str.endsWith("}")){
					JSONObject jObj = new JSONObject(typeValue1Str);
					Object text = jObj.get("text");
					Object ilfo = jObj.get("ilfo");
					if (text != null && ilfo != null) {
						typeItem1 = new MSItem(Integer.parseInt(ilfo.toString()), text.toString());
					}
				}else {
					typeItem1 = (MSItem) this.typeValue;
				}
				if(typeValue2Str.startsWith("{") && typeValue2Str.endsWith("}")){
					JSONObject jObj = new JSONObject(typeValue2Str);
					Object text = jObj.get("text");
					Object ilfo = jObj.get("ilfo");
					if (text != null && ilfo != null) {
						typeItem2 = new MSItem(Integer.parseInt(ilfo.toString()), text.toString());
					}
				}else {
					typeItem2 = (MSItem) typeValue2;
				}
				
				if (typeItem1 == null) {
					return false;
				}
				return typeItem1.equals(typeItem2);
			case "paragraph_msShadow":
				MSShadow shadow1 = null;
				MSShadow shadow2 = null;
				
				String value1 = this.typeValue.toString();
				String value2 = typeValue2.toString();
				
				if(value1.startsWith("{") && value1.endsWith("}")){
					JSONObject jObj = new JSONObject(value1);
					shadow1 = new MSShadow();
					
					Object color = jObj.get("color");
					if (color != null) {
						shadow1.setColor(color.toString());
					}
					Object typeDescription = jObj.get("typeDescription");
					if (typeDescription != null) {
						shadow1.setTypeDescription(typeDescription.toString());
					}
					Object fill = jObj.get("fill");
					if (fill != null) {
						shadow1.setFill(fill.toString());
					}
					Object type = jObj.get("type");
					if (type != null) {
						shadow1.setType(type.toString());
					}
				}else {
					shadow1 = (MSShadow) this.typeValue;
				}
				if(value2.startsWith("{") && value2.endsWith("}")){
					JSONObject jObj = new JSONObject(value2);
					shadow2 = new MSShadow();
					
					Object color = jObj.get("color");
					if (color != null) {
						shadow2.setColor(color.toString());
					}
					Object typeDescription = jObj.get("typeDescription");
					if (typeDescription != null) {
						shadow2.setTypeDescription(typeDescription.toString());
					}
					Object fill = jObj.get("fill");
					if (fill != null) {
						shadow2.setFill(fill.toString());
					}
					Object type = jObj.get("type");
					if (type != null) {
						shadow2.setType(type.toString());
					}
				}else {
					shadow2 = (MSShadow) typeValue2;
				}
				
				if (shadow1 == null) {
					return false;
				}
				
				return shadow1.equals(shadow2);
			case "body_paperSize":
				MSPaperSize paperSize1 = null;
				MSPaperSize paperSize2 = null;
				
				String pValue1 = this.typeValue.toString();
				String pValue2 = typeValue2.toString();
				
				if(pValue1.startsWith("{") && pValue1.endsWith("}")){
					JSONObject jObj = new JSONObject(pValue1);
					paperSize1 = new MSPaperSize();
					
					Object sizeName = jObj.get("sizeName");
					if (sizeName != null) {
						paperSize1.setSizeName(sizeName.toString());
					}
					Object width = jObj.get("width");
					if (width != null) {
						paperSize1.setWidth(Integer.parseInt(width.toString()));
					}
					Object height = jObj.get("height");
					if (height != null) {
						paperSize1.setHeight(Integer.parseInt(height.toString()));
					}
				}else {
					paperSize1 = (MSPaperSize) this.typeValue;
				}
				if(pValue2.startsWith("{") && pValue2.endsWith("}")){
					JSONObject jObj = new JSONObject(pValue2);
					paperSize2 = new MSPaperSize();
					
					Object sizeName = jObj.get("sizeName");
					if (sizeName != null) {
						paperSize2.setSizeName(sizeName.toString());
					}
					Object width = jObj.get("width");
					if (width != null) {
						paperSize2.setWidth(Integer.parseInt(width.toString()));
					}
					Object height = jObj.get("height");
					if (height != null) {
						paperSize2.setHeight(Integer.parseInt(height.toString()));
					}
				}else {
					paperSize2 = (MSPaperSize) typeValue2;
				}
				
				if (paperSize1 == null) {
					return false;
				}
				
				return paperSize1.equals(paperSize2);
			case "body_paragraph":
				Integer[] paragraphs1 = null;
				Integer[] paragraphs2 = null;
				
				String paraValue1 = this.typeValue.toString();
				String paraValue2 = typeValue2.toString();
				
				if(paraValue1.startsWith("[") && paraValue1.endsWith("]")){
					JSONArray jsonArray = new JSONArray(paraValue1);
					paragraphs1 = new Integer[jsonArray.length()];
					Iterator<Object> iterator = jsonArray.iterator();
					int index = 0;
					while(iterator.hasNext()){
						Object next = iterator.next();
						if (next != null) {
							paragraphs1[index++] = Integer.parseInt(next.toString());
						}
					}
				}else {
					paragraphs1 = (Integer[]) this.typeValue;
				}
				if(paraValue2.startsWith("[") && paraValue2.endsWith("]")){
					JSONArray jsonArray = new JSONArray(paraValue2);
					paragraphs2 = new Integer[jsonArray.length()];
					Iterator<Object> iterator = jsonArray.iterator();
					int index = 0;
					while(iterator.hasNext()){
						Object next = iterator.next();
						if (next != null) {
							paragraphs2[index++] = Integer.parseInt(next.toString());
						}
					}
				}else {
					paragraphs2 = (Integer[]) typeValue2;
				}
				
				if (paragraphs1.length == paragraphs2.length && paragraphs1.length == 2
						&& paragraphs1[0] == paragraphs2[0] && paragraphs1[1] == paragraphs2[1]) {
					return Boolean.TRUE;
				}else {
					return Boolean.FALSE;
				}

			default:
				return this.typeValue.equals(typeValue2);
			}
			//return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public Object getTypeValue() {
		return typeValue;
	}

	public void setTypeValue(Object typeValue) {
		this.typeValue = typeValue;
	}

	public String getTypeContent() {
		return typeContent;
	}

	public void setTypeContent(String typeContent) {
		this.typeContent = typeContent;
	}
	
}
